//
//  NSObject+lldb.m
//  QKEasyTest
//
//  Created by ChiCo on 2018/6/4.
//

#import "QKEasyTestLLDB.h"

@implementation QKEasyTestLLDB


/**
 *  获取测试的json文件
 *
 *  @param jsonName 文件名
 *
 *  @return json
 */
+ (id)projectJson:(NSString *)jsonName
{
    
    NSError *error = nil;
    
    NSString *Json_path = [[NSBundle mainBundle] pathForResource:jsonName
                                                          ofType:@"json"];
    
    
    NSData *data=[NSData dataWithContentsOfFile:Json_path];
    
    
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    return jsonObject;
}


+ (NSString *)printObjectOnly:(id)object
{
    if ([NSJSONSerialization isValidJSONObject:object])
    {
        
        NSError *parseError = nil;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
        
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }else
    {
        
        return object;
        
    }
    
    
}

+ (void)printAndSaveObject:(id)object
{
    [self saveStringToDoc:[self printObjectOnly:object]];
    
}

+ (void)saveStringToDoc:(id)str
{
    
    if ([str isKindOfClass:[NSString class]])
    {
        
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path=[paths objectAtIndex:0];
        

        NSString *fileName = [NSString stringWithFormat:@"notes.json"];
        
        NSString *Json_path=[path stringByAppendingPathComponent:fileName];
        NSLog(@"\n%@\n保存JSON到:\n\t%@",str,Json_path);
        
        [jsonData writeToFile:Json_path atomically:YES];
        
    }else
    {
        NSLog(@"没有转化成文本");
    }
    
}

@end
